package com.uphone.magicleafteacher.view;

import android.Manifest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.radish.baselibrary.utils.LogUtils;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.base.OnPermissionCallBack;
import com.uphone.magicleafteacher.util.RecordUtils;
import com.zlw.main.recorderlib.recorder.listener.RecordResultListener;

public class RecordLoadingView1 extends View {
    private final Context mContext;
    private Paint mPaint;
    private Paint mLinePaint;
//    private Paint mBgPaint;
    private float mFirstPosition;
    private int mItemWidth;
    private int mCenterHeight;
    private int defaultPic = R.drawable.ic_recording;
    private int noAnimationPic = R.mipmap.soundrecording;
    //    private boolean isPlaying = false;
    private boolean isFirstLong = false;
    private int mLineWidth = SizeUtils.dp2px(2);
    private int longLineHeight = SizeUtils.dp2px(10);
    private int shortLineHeight = longLineHeight / 2;
    private float longLineStart;
    private float shortLineStart;
    private int position = -1;
    private int mLineSize = 5;
    private int mLinePadding = SizeUtils.dp2px(0);
    private float[][] mLinePosition = new float[mLineSize][4];
    private Bitmap defaultBitmap;
    private int mType;

    public RecordLoadingView1(Context context) {
        this(context, null);
    }

    public RecordLoadingView1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordLoadingView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(defaultBitmap.getWidth(), defaultBitmap.getHeight());
    }

    private void init() {

        mPaint = new Paint();

//        mBgPaint = new Paint();
//        mBgPaint.setColor(getResources().getColor(R.color.themecolor));
//        mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//        mBgPaint.setAntiAlias(true);

        mLinePaint = new Paint();
        mLinePaint.setColor(getResources().getColor(R.color.themecolor));
        mLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mLinePaint.setStrokeWidth(mLineWidth);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeJoin(Paint.Join.ROUND);

        defaultBitmap = BitmapFactory.decodeResource(getResources(), defaultPic);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        LogUtils.e("onDraw");
        if (mType == 0) {
            defaultBitmap = BitmapFactory.decodeResource(getResources(), defaultPic);
            canvas.drawBitmap(defaultBitmap, 0, 0, mPaint);
        } else if (mType == 2) {
            defaultBitmap = BitmapFactory.decodeResource(getResources(), noAnimationPic);
            canvas.drawBitmap(defaultBitmap, 0, 0, mPaint);
        } else {
//            RectF rectF = new RectF(0, 0, defaultBitmap.getWidth(), defaultBitmap.getHeight());
//            canvas.drawOval(rectF, mBgPaint);
            if (mFirstPosition <= 0) {
                mItemWidth = (defaultBitmap.getWidth() - (mLinePadding * 2)) / mLineSize;
                mFirstPosition = (mItemWidth - mLineWidth) / 2 + mLinePadding;
                mCenterHeight = defaultBitmap.getHeight() / 2;
                longLineStart = mCenterHeight - (longLineHeight / 2);
                shortLineStart = mCenterHeight - (shortLineHeight / 2);

                for (int i = 0; i < mLineSize; i++) {
                    float startY = (i % 2 == 1) ? shortLineStart : longLineStart;
                    float endY = (i % 2 == 1) ? shortLineStart + shortLineHeight : longLineStart + longLineHeight;
                    mLinePosition[i] = new float[]{i * mItemWidth + mFirstPosition, startY, i * mItemWidth + mFirstPosition/* + mLineWidth*/, endY};
                }
            }
            if (position > -1) {
                Float aFloat = mLinePosition[position][1];
                if (aFloat == shortLineStart) {
                    mLinePosition[position][1] = longLineStart;
                    mLinePosition[position][3] = longLineStart + longLineHeight;
                } else {
                    mLinePosition[position][1] = shortLineStart;
                    mLinePosition[position][3] = shortLineStart + shortLineHeight;
                }
            }
            position = (position + 1) % mLineSize;
            for (int i = 0; i < mLineSize; i++) {
                canvas.drawLines(mLinePosition[i], mLinePaint);
            }
            mHandler.sendEmptyMessageDelayed(0, 100);
        }
    }


    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (mType == 1) {
                invalidate();
            }
        }
    };

    public int getType() {
        return mType;
    }

    public void setPlaying(int type) {
        mType = type;
        invalidate();
    }

    public void setPlaying(BaseGActivity activity, int type) {
        if (type == 0) {//默认状态
            this.mType = type;
            invalidate();
        } else if (type == 1) {//动画状态
            activity.getPermission(
                    new OnPermissionCallBack() {
                        @Override
                        public void permissionPass(String[] permissions) {
                            mType = type;
                            invalidate();
                            // 录音
                            RecordUtils.init().doPlay();
                        }

                        @Override
                        public void permissionRefuse(String[] permissions) {
                            ToastUtils.showShort("您已禁止权限，请到设置中心打开权限");
                        }
                    }, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO);
        } else if (type == 2) {//绘制不动画状态
            invalidate();
            this.mType = type;
            invalidate();
            // 结束
            RecordUtils.init().doStop(null);
        }
    }

    public void setPlaying(BaseGActivity activity, int type, RecordResultListener listener) {
        if (type == 0) {//默认状态
            this.mType = type;
            invalidate();
        } else if (type == 1) {//动画状态
            activity.getPermission(
                    new OnPermissionCallBack() {
                        @Override
                        public void permissionPass(String[] permissions) {
                            mType = type;
                            invalidate();
                            // 录音
                            RecordUtils.init().doPlay();
                        }

                        @Override
                        public void permissionRefuse(String[] permissions) {
                            ToastUtils.showShort("您已禁止权限，请到设置中心打开权限");
                        }
                    }, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO);
        } else if (type == 2) {//绘制不动画状态
            invalidate();
            this.mType = type;
            invalidate();
            // 结束
            RecordUtils.init().doStop(listener);
        }
    }
}
